from baidusearch.baidusearch import search
import re

def clean_abstact(text: str):
    """
    清洗文本
    """
    # 将换行符转为空格
    text = text.replace('\n', ' ')
    # 将连续空格改为单个空格
    text = re.sub(r'\s+', ' ', text)
    return text.strip()

def web_search(query):
    """
    直接搜索无需api
    """
    results = search(query, num_results=10)
    for item in results:
        item['abstract'] = clean_abstact(item['abstract'])

    return results

if __name__ == "__main__":
    search_result = web_search("特朗普与马斯克的冲突")
    print(search_result)